package ecom.shell.commandsImpl;

import ecom.beans.EcomCustomerRemote;
import ecom.beans.EuroConvertorBean.Currency;
import ecom.shell.commandsImpl.OutputCommandImpl.Outputs;
import java.io.PrintStream;
import java.util.List;
import java.util.StringTokenizer;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import shell.ShellCommand;
import shell.ShellContext;

public class CurrencyCommandImpl implements ShellCommand {

    private ShellContext _context = null;

    public CurrencyCommandImpl(ShellContext context) {
        _context = context;
    }

    @Override
    public String getName() {
        return "currency";
    }

    @Override
    public String getUsage() {
        StringBuffer sb = new StringBuffer();

        EcomCustomerRemote ecomCustomerBean = (EcomCustomerRemote) _context.getVar("customerBean");
        List<String> symbols = ecomCustomerBean.getCurrencySymbols();

        for (int i = 0; i < symbols.size(); ++i) {
            sb.append(symbols.get(i));
            if (i < symbols.size() - 1) {
                sb.append("|");
            }
        }
        return "currency " + sb;
    }

    @Override
    public String getShortDescription(){
        return "Set currency\n";
    }

    @Override
    public String execute(String cmdline, PrintStream out, PrintStream err) {
        StringBuilder res = new StringBuilder();
        EcomCustomerRemote ecomCustomerBean = (EcomCustomerRemote) _context.getVar("customerBean");

        StringTokenizer st = new StringTokenizer(cmdline, " ");

        st.nextToken();

        if (st.countTokens() == 0) {
            try {
                List<String> symbols = ecomCustomerBean.getCurrencySymbols();
                Outputs currentOutput = (Outputs) _context.getVar("output");
                return printList(out, symbols, currentOutput, ecomCustomerBean);
            } catch (Exception ex) {
                err.println("Error with currencies display");
            }
        }
        else if (st.countTokens() == 1) {
            String newCurrency = st.nextToken().trim();
            Currency c;
            try {
                c = ecomCustomerBean.getCurrencyByName(newCurrency);
                res.append("New Currency : ").append(c).append(" (").append(ecomCustomerBean.getCurrencyRealName(String.valueOf(c))).append(")");
                _context.setVar("currency", c);
            } catch (Exception ex) {
                err.println(ex + " for '" + getName() + "' command");
            }
        } else {
            err.println("\t" + EcomCommandsConstantes.INCORRECT_NUMBER_ARGS);
        }
        return res.toString();
    }

    private String printList(PrintStream out, List<String> symbols, Outputs currentOutput, EcomCustomerRemote ecomCustomerBean) throws Exception {
        StringBuilder res = new StringBuilder();
        if (currentOutput.equals(Outputs.XML)) {
            Element currencies = new Element("currencies");
            org.jdom.Document document = new Document(currencies);

            for (String currency : symbols) {
                currencies.addContent(buildXMLNode(ecomCustomerBean.getCurrencyRealName(currency), currency));
            }
            XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
            return sortie.outputString(document);

        } else if (currentOutput.equals(Outputs.TEXT)) {
            for (String c : symbols) {
                res.append("\t").append(c).append(" - ").append(ecomCustomerBean.getCurrencyRealName(c)).append("\n");
            }
        } else {
            res.append("Output format '").append(currentOutput).append("' is not implementing for this command");
        }
        return res.toString();
    }

    private Element buildXMLNode(String nameDevise, String idDevise) {
        Element currency = new Element("currency");
        Element name = new Element("name");
        Attribute id = new Attribute("id", idDevise);
        name.setAttribute(id);
        name.setText(nameDevise);
        currency.addContent(name);
        return currency;
    }
}
